package com.java110.mall.chain.cmd.chainProduct;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.chain.ChainSupplierProductDto;
import com.java110.intf.chain.IChainCoreInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Java110Cmd(serviceCode = "chainProduct.queryProductDeatil")
public class QueryProductdDetailCmd extends Cmd {

    @Autowired
    private IChainCoreInnerServiceSMO chainCoreInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        super.validatePageInfo(reqJson);
        Assert.hasKeyAndValue(reqJson, "csId", "未包含供应商");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        ChainSupplierProductDto chainSupplierProductDto = BeanConvertUtil.covertBean(reqJson, ChainSupplierProductDto.class);
        ResultVo resultVo = null;
        try {
            resultVo = chainCoreInnerServiceSMOImpl.querySingleProduct(chainSupplierProductDto);
        } catch (Exception e) {
            e.printStackTrace();
            cmdDataFlowContext.setResponseEntity(ResultVo.error(e.getMessage()));
            return;
        }

        cmdDataFlowContext.setResponseEntity(new ResponseEntity(resultVo.toString(), HttpStatus.OK));
    }
}
